package fr.umlv.m2.recipes.ideas;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TextView;
import fr.umlv.m2.recipes.ideas.bean.StepBean;
import fr.umlv.m2.recipes.ideas.manager.SoundManager;
/**
 * Create the StepView Activity
 * @author Christophe Chillan et Younes Chandarli
 *
 */
public class StepView extends Activity implements OnChronometerTickListener {
	private TextView titleStep;
	private TextView descStep;
	private Button previousButton;
	private Button nextButton;
	private int index;
	private ArrayList<StepBean> steps;
	private Chronometer stepChronometer;
	private SoundManager soundManager;
	private long elapsedTime;
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		TextView textview = new TextView(this);
	    textview.setText("rubrique des étapes");
	    setContentView(R.layout.step);
	    Bundle extras = getIntent().getExtras(); 
	    if(extras !=null)
	    {
	    	soundManager = new SoundManager();
	    	soundManager.initSounds(getBaseContext());
	    	soundManager.addSound(1, R.raw.yeah);
	    	@SuppressWarnings("unchecked")
			ArrayList<StepBean> stepList = (ArrayList<StepBean>) getIntent().getExtras().getSerializable("steps");
	    	steps = stepList;
	    	stepChronometer = (Chronometer) findViewById(R.id.chrono);
	    	stepChronometer.setOnChronometerTickListener(this);
	    	titleStep = (TextView) findViewById(R.id.titleStep);
	    	descStep = (TextView) findViewById(R.id.descStep);
	    	descStep.setMovementMethod(new ScrollingMovementMethod());
	    	index=0;
	    	previousButton = (Button) findViewById(R.id.previous);
	    	nextButton = (Button) findViewById(R.id.next);
	    	
	    	nextButton.setOnClickListener(nextButtonListener());
	    	previousButton.setOnClickListener(previousButtonListener());
	    	
	    	previousButton.setEnabled(false);
	    	initializeTime(steps.get(index).getDuration()*60000);
	    	stepChronometer.playSoundEffect(R.raw.yeah);
	    	titleStep.setText(steps.get(index).getName());
	    	descStep.setText(steps.get(index).getDescription());
	    }
	}
	

	private OnClickListener nextButtonListener() {
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				previousButton.setEnabled(true);
				
				if(index<steps.size()-1){
					index++;
					nextButton.setEnabled(true);
				}
				
				nextButton.setOnClickListener(nextButtonListener());
				titleStep.setText(steps.get(index).getName());
		    	descStep.setText(steps.get(index).getDescription());
		    	initializeTime(getRestOfTime());
		    	if(index==steps.size()-1){					
					nextButton.setEnabled(false);
		    	}
				
			}
		};
	}


	private OnClickListener previousButtonListener() {
		return new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				nextButton.setEnabled(true);
				if(index>0){
					index--;
					previousButton.setEnabled(true);
				}
				
				previousButton.setOnClickListener(previousButtonListener());
				titleStep.setText(steps.get(index).getName());
		    	descStep.setText(steps.get(index).getDescription());
		    	initializeTime(getRestOfTime());
		    	if(index==0){
					previousButton.setEnabled(false);
				}
			}
		};
	}
	
	/**
	 * Use to start the chronometer
	 * @param target
	 */
	public void startChrono(View target) {
		stepChronometer.setBase(SystemClock.elapsedRealtime());
		stepChronometer.start();
	}
	
	/**
	 * Use to stop the chronometer
	 * @param target
	 */
	public void stopChrono(View target) {
		stepChronometer.stop();
		elapsedTime=0;
		initializeTime(steps.get(index).getDuration()*60000);
		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainmenu, menu);
		return true;
	}

	/**
	 * Use to Define the Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		// On regarde quel item a été cliqué grâce à son id et on déclenche une
		// action
		switch (item.getItemId()) {
			case R.id.home:
				startActivity(new Intent(this, MenuTabWidget.class));
				return true;
			case R.id.about:
				startActivity(new Intent(this, AboutView.class));
				return true;
			default:
		        return super.onOptionsItemSelected(item);
			}
		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onChronometerTick(Chronometer chronometer) {
		elapsedTime = SystemClock.elapsedRealtime() - chronometer.getBase();
		long restOfTime = getRestOfTime();
		if(restOfTime<=0){
			soundManager.playSound(1);
			stepChronometer.stop();
		}
	    initializeTime(restOfTime);

		
	}
	
	
	private void initializeTime(long restOfTime){
		int min = (int)(restOfTime/60000);
	    int sec = (int) ((restOfTime/1000) % 60);
	    String time = min<10? "0" + min : String.valueOf(min);
	    time += ":";
	    time += sec<10? "0" + sec : String.valueOf(sec);
	    stepChronometer.setText(time);
		
	}
	
	private long getRestOfTime(){
		long restOfTime = (steps.get(index).getDuration()*60000)-elapsedTime;
		return restOfTime;
	}
	
	

}
